var color=[["#fe8e08","#ffc501","#b39ddb","#1da6da","#5dc3c7"],["#ffc501","#1da6da","#b39ddb"],["#ffc501","#1da6da","#b39ddb"]];


/**
	1 在役管段  查询内检测环焊缝，弯头数据
    0 非在役管段 查询基础环焊缝，冷弯头，煨弯头
 */
var enlist=1;
/**
 * 页面初始化执行js
 */
$(function(){
//	submitForm(1,10);
	$(".selectLine a").click(function(){
		resultAjax(getJsPath()+"../comm/taskSegment.json","lineId="+$(this).attr("id"),setTaskSegment,"JSON");
		getSendBall($(this).attr("id"));
	});
	$(".exportExlBtn").click(function(){
		$('.zy-i-input').css({border:"none"});
		$('.zy-i-input').css({height:"35px"});
		var lineName=window.line.val('text');
		var taskName=window.task.val('text');
		var name = lineName+taskName+$(this).attr("exportName");


		resultAjax(path+"/comm/checkOutTime.json","",function(data,name){
			var exportFile = $(this).EXPT({
				name:name ,
				yesBtnAct: function(callback){
					exportPDF(exportFile.name);
					callback();
				}
			});

		},"json",name);
	});
	$(".find").click(function(){
		window.isDownPDF=true;
		searchData();
	});
	
	tabHandler();
	$(".defect-down img").each(function(){
		if($(this).attr("style") != undefined)
		{
			$(this).parent().attr("imgsty",$(this).attr("style"))
		}
		$(this).parent().css("text-align","center");
		$(this).parent().attr("img",path+"/static/img/chart_no_data.png");
		$(this).attr("src",path+"/static/img/chart_no_data.png")
	});
	

	$(".right-report.clearfix div[id]").each(function(){
		if($(this).parent().hasClass("w100"))
		{
			$(this).css("width",$(".right-report.clearfix").width()-50);
		}else{
			$(this).css("width",($(".right-report.clearfix").width()-50)/2);
		}
	});
	bindBall();
});

/**
 * 导出pdf
 * @param fileName
 */
function exportPDF(fileName)
{

	if(window.isDownPDF)
	{
		var args=[];
		$(".defect-down").each(function(){
			args.push(this);
		});


		var obj = [window.line,window.task];
		if(checkForm(obj)&&checkFormVerify($("#JQ_form"))) {

			createImg(args,function (data){
				var lineName=window.line.val('text');
				var taskName=window.task.val('text');

				//var param=[];
				//var html="";
				//for(var i=0;i< data.length-1;i++)
				//{
				//	html+="<img src='"+data[i]+"' />"
                //
				//}
				//param.push({value:html,name:"htmls"});
				//html="<img src='"+data[2]+"' />";
				//param.push({value:html,name:"htmls"});
				//param.push({value:lineName+taskName+"其它缺陷统计",name:"name"});
				//param.push({value:fileName,name:"fileName"});
				if(checkForm(obj)&&checkFormVerify($("#JQ_form"))) {
					downPDF(args, lineName + taskName + "其它缺陷统计图", resetData, fileName);
				}
				//resultAjax(_path+"/comm/checkOutTime.json","",function(data,param){
				//	s_downFile(_path+"/comm/downHtmlPdf.json",param);
				//},"json",param);
				//resetData();
				//"其它缺陷",resetData
			});

		}

	}

}


function searchData()
{
	var lineIdVal=window.line.val();
	var taskIdVal=window.task.val();
	var beginMileageVal=window.begin.val('mile');
	var endMileageVal=window.end.val('mile');



	var obj = [window.line,window.task,window.begin,window.end];

	if(checkForm(obj)&&checkFormVerify($("#JQ_form")))
	{
		var data="lineId="+lineIdVal+"&taskId="+taskIdVal;
		data+="&beginMileage="+beginMileageVal+"&endMileage="+endMileageVal;
		resultAjax(getJsPath()+"../insp/statistics/OtheDefectController/findStatistics.json",data,findResult,"JSON");

	}


}

/**
 * 处理tab 点击事件
 */
function tabHandler(){
	$(".left-table table tr td a").attr("href","javascript:;");
	$(".left-table table tr td a").live("click",function(){

		//var lineId=$("div.dropdown-list[data-name='lineId']").find("a").attr("id");
		//var taskSegmentId=$("div.dropdown-list[data-name='taskSegmentId']").find("a").attr("id");
		//var taskId=$("div.dropdown-list[data-name='taskId']").find("a").attr("id");
		//var segmentId=$("div.dropdown-list[data-name='segmentId']").find("a").attr("id");


		var lineId=window.line.val();
		var taskId=window.task.val();
		var beginMileage=window.begin.val('mile');
		var endMileage=window.end.val('mile');

		/*
		 var segment=$(".ck1[data-name] span.check-box .active");
		 var segmentId=segment.parent().parent().attr("data");
		 */
		if($(this).attr("to") != undefined && typeof(lineId) != "undefined" &&
			typeof(taskId) != "undefined" && lineId != ""
			&& taskId != ""
		){
			var url=$(this).attr("to");//+"&lineId="+lineId+"&taskId="+taskId+"&beginMileage="+beginMileage+"&endMileage="+endMileage;
			var param=[];
			param.push({name:"lineId",value:lineId});
			param.push({name:"taskId",value:taskId});
			param.push({name:"beginMileage",value:beginMileage});
			param.push({name:"endMileage",value:endMileage});
			param.push({name:"beginMileageName",value:window.begin.val("text")});
			param.push({name:"endMileageName",value:window.end.val("text")});
			if($(this).text() != "0"){
				//alert("ssss");
				//window.location.href=url;
				postGo(url,param)
			}

		}


	});
}
function resetData()
{
	findResult(window.prevData);
}

/**
 * 
 * 查询数据返回
 * @param data
 */
function findResult(data)
{
	if(data.result != undefined)
	{
		data=data.result;
	}

	window.prevData=data;
	//其他统计饼状图数据
	var otherPieData= [];
	var otherPieName= ["凹陷","焊缝异常","椭圆变形","类裂纹缺陷","其它损伤缺陷"];
	var otherName= [];

	//凹陷饼状图数据
	var depressedPieData= [];
	var depressedPieName= ["小于2%OD","2%OD -6%OD","6%OD以上"];
	var depressedName=[];
	
	//凹陷饼状图数据
	var girthWeldPieData= [];
	var girthWeldPieName= ["环焊缝异常数量","螺旋焊缝异常数量","直焊缝异常数量"];
	var girthWeldName=[];
	var depressNum=0;
	//凹陷列表
	$("#depressedTab tr").each(function(index){
		if(index<6)
		{
			$(this).find("td:eq(1) a").html(data.depressedList[index].NUM);
			if(index > 0 && index < 4 )
			{
				depressedPieData.push({name:depressedPieName[index-1],value:parseInt(data.depressedList[index].NUM),color:color[0][index-1]});
				depressedName.push(depressedPieName[index-1]);
				depressNum+=parseInt(data.depressedList[index].NUM);
			}
		}
		
		
	});
	if(depressNum >0)
	{
		$("#depressedTab tr:eq(6) td:eq(1)").html("1");
	}else{
		$("#depressedTab tr:eq(6) td:eq(1)").html("0");

	}
	//其他统计列表
	var countNum=0;
	$("#otherTab tr").each(function(index){
		if(index >0)
			$(this).find("td:eq(1) a").html(data.otherList[index-1].NUM);
		if(index>0 )
		{
			countNum+=parseInt(data.otherList[index-1].NUM);
			
			otherPieData.push({name:otherPieName[index-1],value:parseInt(data.otherList[index-1].NUM),color:color[1][index-1]});
			otherName.push(otherPieName[index-1]);
		}
		
	});

	$("#otherTab tr:eq(0) td:eq(1)").html(countNum);

	//焊缝统计列表
	countNum=0;
	$("#girthWeldTab tr").each(function(index){
		if(index >0)
			$(this).find("td:eq(1) a").html(data.girthWeldList[index-1].NUM);
		if(index>0 )
		{
			countNum+=parseInt(data.girthWeldList[index-1].NUM);
			
			girthWeldPieData.push({name:girthWeldPieName[index-1],value:parseInt(data.girthWeldList[index-1].NUM),color:color[2][index-1]});
			girthWeldName.push(girthWeldPieName[index-1]);
		}
		
	});
	
	$("#girthWeldTab tr:eq(0) td:eq(1) a").html(countNum);
	
	
	//环焊缝统计图 begin
	//drawBar(data.otherhList,"hhBar");
	//drawBar(data.otherzgList,"lxhfBar");
	drawBar(data.depressedBar,"depressedBar");
	drawBar(data.girthWeldBar,"girthWeldBar");



	//其他缺陷时钟分布图
	drawTimeCharts(data.otherClick,"#depressedClick1");

	//凹陷时钟分布图
	drawTimeCharts(data.depressedClick,"#depressedClick2");


	//焊缝时钟分布图
	drawTimeCharts(data.girthWeldClick,"#girthWeldClick");


	//
	//var picECharts = new PicEChart();
	//var data1= new Array();
	//var Axisdata= new Array();
	//for(var i=0;i<data.otherhList.length;i++){
	//	Axisdata.push(parseFloat(data.otherhList[i].NAME).toFixed(0));
	//	var num =data.otherhList[i].NUM;
	//	num=num=="-"?"0":num;
	//	data1.push(parseInt(num));
	//
	//}
	//
	//try{
	//	if(data1.length >0 )
	//	{
	//		addUnit("#hhBar");
	//		picECharts.addbar({title:'',name1:'环焊缝按里程统计图',Axisdata:Axisdata,data:data1,divObject:'hhBar',type:"bar"});
	//		$("#hhBar").parent().find("i").remove();
	//	}else{
	//		$("#hhBar").html("<img src='"+$("#hhBar").attr("img")+"'   />");
	//		//$("#hhBar").parent().append("<i></i>");
	//	}
	//}catch(e){
	//	console.log(e);
	//}
	//环焊缝统计图 end


	//直焊缝按里程等分统计图 begin
	//picECharts = new PicEChart();
	//data1= new Array();
	//Axisdata=new Array();
	//for(var i=0;i<data.otherzgList.length;i++){
	//	Axisdata.push(parseFloat(data.otherzgList[i].NAME).toFixed(0));
	//	var num =data.otherzgList[i].NUM;
	//	num=num=="-"?"0":num;
	//	data1.push(parseInt(num));
	//}
	//
	//try{
	//	if(data1.length >0 )
	//	{
	//		addUnit("#lxhfBar");
	//		picECharts.addbar({title:'',name1:'直焊缝按里程等分统计图',Axisdata:Axisdata,data:data1,divObject:'lxhfBar'});
	//		$("#lxhfBar").parent().find("i").remove();
	//	}else{
	//		$("#lxhfBar").html("<img src='"+$("#lxhfBar").attr("img")+"'  />");
	//		//$("#lxhfBar").parent().append("<i></i>");
	//	}
	//}catch(e){
	//	console.log(e);
	//}
	//直焊缝按里程等分统计图 end

	//凹陷按里程等分统计图 begin
	//picECharts = new PicEChart();
	//data1= new Array();
	//Axisdata=new Array();
	//for(var i=0;i<data.depressedBar.length;i++){
	//	Axisdata.push(parseFloat(data.depressedBar[i].NAME).toFixed(0));
	//	var num =data.depressedBar[i].NUM;
	//	num=num=="-"?"0":num;
	//	data1.push(parseInt(num));
	//}
	//
	//try{
	//	if(data1.length >0 )
	//	{
	//		addUnit("#depressedBar");
	//		picECharts.addbar({title:'',name1:'凹陷按里程等分统计图',Axisdata:Axisdata,data:data1,divObject:'depressedBar'});
	//		$("#depressedBar").parent().find("i").remove();
	//	}else{
	//		$("#depressedBar").html("<img src='"+$("#depressedBar").attr("img")+"'   />");
	//		//$("#depressedBar").parent().append("<i></i>");
	//	}
	//}catch(e){
	//	console.log(e);
	//}
	//凹陷按里程等分统计图 end

	//焊缝按里程等分统计图 begin
	//picECharts = new PicEChart();
	//data1= new Array();
	//Axisdata=new Array();
	//for(var i=0;i<data.girthWeldBar.length;i++){
	//	Axisdata.push(parseFloat(data.girthWeldBar[i].NAME).toFixed(0));
	//	var num =data.girthWeldBar[i].NUM;
	//	num=num=="-"?"0":num;
	//	data1.push(parseInt(num));
	//}
	//try{
	//	if(data1.length >0 )
	//	{
	//		addUnit("#girthWeldBar");
	//		picECharts.addbar({title:'',name1:'焊缝按里程等分统计图',Axisdata:Axisdata,data:data1,divObject:'girthWeldBar'});
	//		$("#girthWeldBar").parent().find("i").remove();
	//	}else{
	//		$("#girthWeldBar").html("<img src='"+$("#girthWeldBar").attr("img")+"'  />");
	//		//$("#girthWeldBar").parent().append("<i></i>");
	//	}
	//}catch(e){
	//	console.log(e);
	//}
	//焊缝按里程等分统计图 end

	try{
		var num=0;
		for (var info in depressedPieData)
		{
			num+=depressedPieData[info].value;
			depressedPieData[info].name = depressedPieData[info].name.replace("数量","");
		}

		if(num > 0)
		{
			//凹陷异常饼状图
			picECharts = new PicEChart();
			picECharts.addHollowPieByML({legends:depressedName,legendX:"50%",legendY:"15%",data:depressedPieData,divObject:'depressedPie'});
			$("#depressedPie").parent().find("i").remove();
		}else{
			$("#depressedPie").html("<img src='"+$("#depressedPie").attr("img")+"' />");
			//$("#depressedPie").parent().append("<i></i>");
		}
	}
	catch(e)
	{
		console.log(e);
	}
	try{
		var num=0;
		for (var info in otherPieData)
		{
			num+=otherPieData[info].value;
			otherPieData[info].name = otherPieData[info].name.replace("数量","");
		}

		if(num > 0)
		{
			//饼状图
			picECharts = new PicEChart();
			picECharts.addHollowPieByML({legends:otherName,legendX:"50%",legendY:"15%",data:otherPieData,divObject:'otherPie'});
			$("#otherPie").parent().find("i").remove();
		}else{
			$("#otherPie").html("<img src='"+$("#otherPie").attr("img")+"' />");
			//$("#otherPie").parent().append("<i></i>");
		}
	}
	catch(e)
	{
		console.log(e);
	}
	try{
		var num=0;
		for (var info in girthWeldPieData)
		{
			num+=girthWeldPieData[info].value;
			girthWeldPieData[info].name = girthWeldPieData[info].name.replace("数量","");
		}

		if(num > 0)
		{
			//焊缝统计饼状图
			picECharts = new PicEChart();
			picECharts.addHollowPieByML({legends:girthWeldName,legendX:"50%",legendY:"15%",data:girthWeldPieData,divObject:'girthWeldPie'});
			$("#girthWeldPie").parent().find("i").remove();
		}else{
			$("#girthWeldPie").html("<img src='"+$("#girthWeldPie").attr("img")+"' />");
			//$("#girthWeldPie").parent().append("<i></i>");
		}
	}
	catch(e)
	{
		console.log(e);
	}






}

function addUnit(box)
{
	var html_='<em class="dw">(m)</em>';
	$(box).parent().find(".dw").remove();
	$(box).append(html_);
}

/**
 * 绘制时钟位置图
 * @param data
 * @param id
 */
function drawTimeCharts(data,id)
{
	var clickJson=[];
	var count = 0;
	var h12=0;
	for(var i=0;i<data.length;i++){
		var t=data[i].T;
		var num =data[i].NUM;
		num= (num=="-"|| num==null )?"0":num;
		if(t != "12")
		{

			clickJson.push({"t":(parseInt(t)+1),"val":num,"color":parseInt(t)});
		}else{
			h12=num;
		}
		count += num;
	}

	for(var i=0;i<clickJson.length;i++)
	{
		if(clickJson[i].t==1)
		{
			clickJson[i].val=parseInt(clickJson[i].val)+parseInt(h12);
		}
	}

	if(count == 0)
	{
		$(id).html("<img src='"+$(id).attr("img")+"'  />");
	}else{
		new createTimeMap(id,clickJson,"",function(d){});

	}
}


function drawBar(data,box)
{
	//$("#"+box).attr("img",path+"/static/chart_no_data.png")
	$('#'+box).css("height",$('#'+box).parent().height()-70);

	$('#'+box).parent().find(".dw").remove();
	var html_='<em class="dw">(m)</em>';
	var totale=0;
	var Axisdata=[];
	var showdata=[];
	for(var i=0;i<data.length;i++){
		Axisdata.push(parseFloat(data[i].NAME).toFixed(0));
		var num =data[i].NUM;
		num=num==null?"0":num;
		num=parseInt(num);
		showdata.push(num);
		totale+=num;
	}
	if(totale >0 )
	{
		var picECharts = new PicEChart();

		drawClickBar(Axisdata,showdata,box);
		//picECharts.addbar({name1:'降水量',Axisdata:Axisdata,data:showdata,divObject:box});
		$("#"+box).parent().find("i").remove();
		$("#"+box).css({position:"relative"});
		window.setTimeout(function(){$('#'+box).append(html_)},500);


	}else{
		$("#"+box).html("<img src='"+$("#"+box).attr("img")+"'  />");
		//$("#"+box).parent().append("<i></i>");
	}

}


drawClickBar=function(mileageList,data,id)
{
	mileageList = handlerMileage(mileageList);
	var picECharts = new PicEChart();
	picECharts.addMorebar({name1:'对比统计图',num:1,Axisdata:mileageList,data:data,divObject:id,legend:mileageList,onClickCall:function() {

	}});
};


window.isbh = "_";//修改了echarts 源代码

function handlerMileage(data1)
{
	var mileageList = [];
	var t = null;
	var ts = null;
	for(var i=0,e=data1.length;i<e;i++)
	{
		var v = Math.round(parseFloat(data1[i]))+"";
		if( i+1 <e )
		{
			if(parseFloat(data1[i+1])  - parseFloat(data1[i])  <10)
			{

				// v=v.substring(0, v.length) ;
			}else if(parseFloat(data1[i+1])  - parseFloat(data1[i])  <100)
			{

				v=v.substring(0, v.length -1) +"0";
			}else if(parseFloat(data1[i+1])  - parseFloat(data1[i])  >=100)
			{
				v=v.substring(0, v.length -2) +"00";
			}
			t = data1[i];
		}else{
			if(parseFloat(data1[i])  - parseFloat(t)  <10)
			{

				// v=v.substring(0, v.length) ;
			}else if(parseFloat(data1[i])  - parseFloat(t)  <100)
			{
				v=v.substring(0, v.length -1) +"0";

			}else if(parseFloat(data1[i])  - parseFloat(t)  >=100)
			{
				v=v.substring(0, v.length -2) +"00";

			}
		}

		if(ts != v)
		{
			t = v;
			mileageList.push(parseFloat(v));
		}else{
			mileageList.push("");
		}
		ts =v;
	}

	return mileageList;
}